package middol.base

import grails.gorm.transactions.Transactional
import middol.constants.i18n.CommonConstants
import middol.result.ResultUtils
import middol.utils.DataBaseService

@Transactional
class DocsService {
    DataBaseService dataBaseService
    /**
     * 获取数据字典
     */
    def getDataDic() {

        String dicSqlStr = """
            SELECT
                C.TABLE_SCHEMA AS TABLE_SCHEMA,
                T.TABLE_NAME AS TABLE_NAME,
                T.TABLE_COMMENT AS TABLE_COMMENT,
                C.COLUMN_NAME AS COLUMN_NAME,
                C.COLUMN_COMMENT AS COLUMN_COMMENT,
                C.ORDINAL_POSITION AS ORDINAL_POSITION,
                C.COLUMN_DEFAULT AS COLUMN_DEFAULT,
                C.IS_NULLABLE AS IS_NULLABLE,
                C.DATA_TYPE AS DATA_TYPE,
                C.CHARACTER_MAXIMUM_LENGTH AS CHARACTER_MAXIMUM_LENGTH,
                C.NUMERIC_PRECISION AS NUMERIC_PRECISION,
                C.NUMERIC_SCALE AS NUMERIC_SCALE,
                C.COLUMN_TYPE AS COLUMN_TYPE,
                C.COLUMN_KEY COLUMN_KEY,
                C.EXTRA AS EXTRA
            FROM
                information_schema.`TABLES` T
            LEFT JOIN information_schema.`COLUMNS` C ON T.TABLE_NAME = C.TABLE_NAME
            AND T.TABLE_SCHEMA = C.TABLE_SCHEMA
            WHERE
                T.TABLE_SCHEMA = 'app' 
            ORDER BY
                C.TABLE_NAME,
                C.ORDINAL_POSITION;
        """

        def result = dataBaseService.queryForList(dicSqlStr)
        String fileName = "数据字典.md"
        def tableNameList = result.collect {
            [
                    TABLE_NAME: it.TABLE_NAME,
                    TABLE_COMMENT: it.TABLE_COMMENT
            ]
        }.unique()
        File dicFile = new File(fileName)
        dicFile.withPrintWriter { printWriter ->
            tableNameList.each { table ->
                printWriter.println("## 【表】 ${table.TABLE_COMMENT} ${table.TABLE_NAME}")
                printWriter.println("| 列名 | 列注释 | 列的排序 | 默认值 | 是否为空 | 数据类型 | 字符最大长度 | 数值精度(最大位数) | 小数精度 | KEY |")
                printWriter.println("| --- | ----- | --------| ----- | ------ | ------ | ---------- | --------------- | ------ | --- |")
                result.findAll {
                    it.TABLE_NAME == table.TABLE_NAME
                }.each { item ->
                    int index = CommonConstants.COMMON_COLUMN.indexOf(item.COLUMN_NAME)
                    if(index > -1){
                        item.COLUMN_COMMENT = CommonConstants.COMMON_COLUMN_DESC[index]
                    }
                    printWriter.println("| ${item.COLUMN_NAME ?: ''} | ${item.COLUMN_COMMENT ?: ''} | ${item.ORDINAL_POSITION ?: ''} | ${item.COLUMN_DEFAULT ?: ''} | ${item.IS_NULLABLE ?: ''} | ${item.DATA_TYPE ?: ''} | ${item.CHARACTER_MAXIMUM_LENGTH ?: ''} | ${item.NUMERIC_PRECISION ?: ''} | ${item.NUMERIC_SCALE ?: ''} | ${item.COLUMN_KEY ?: ''} | ${item.EXTRA ?: ''} |".toString())
                }
            }

        }
        return dicFile
    }
}
